package com.rqf.ls.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.rqf.ls.service.IManagementUserService;
import com.server.password.MyMD5;

@Service("ManagementUserService")
public class ManagementUserService implements IManagementUserService {
	
	//管理员信息
	private Map<String, String> managers = null;
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	 /**
	 * 初始化后台管理员
	 * @author ServerZhang
	 * @date 2016年5月10日
	 */
	@Override
	public void initManager(){
		this.managers = new HashMap<String, String>();
		String password1 = "666666";
		String password2 = "666666";
		String password3 = "666666";
		String encrypt1 = MyMD5.circularlyEncrypt(password1, 3);
		String encrypt2 = MyMD5.circularlyEncrypt(password2, 3);
		String encrypt3 = MyMD5.circularlyEncrypt(password3, 3);
		managers.put("admin", encrypt1);
		managers.put("root", encrypt2);
		managers.put("manager", encrypt3);
	}
	
	@Override
	public Map<String, String> getManagers(){
		return this.managers;
	}
	
	/**
	 * 管理员登录验证
	 * @param name
	 * @param password
	 * @return
	 * @author ServerZhang
	 * @date 2016年5月10日
	 */
	@Override
	public Boolean login(String name, String password){
		Boolean verify = false;
		if (StringUtils.isEmpty(name) || StringUtils.isEmpty(password)) {
			return verify;
		}
		//MD5经过3次加密
		String encryptStr = MyMD5.circularlyEncrypt(password, 3);
		
		if (this.managers != null && encryptStr.equals(this.managers.get(name))) {
			verify = true;
		}
		return verify;
	}
	@Override
	public Boolean verifySession(String name, String encryptStr){
		Boolean verify = false;
		if (StringUtils.isEmpty(name) || StringUtils.isEmpty(encryptStr)) {
			return verify;
		}
		if (this.managers != null && encryptStr.equals(this.managers.get(name))) {
			verify = true;
		}
		return verify;
	}
}
